﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Agr.Questionnaire.Core.Domain.Qnnaire.Commands;
using Agr.Questionnaire.Core.ViewModel.Qnnaire;

namespace Agr.Questionnaire.Frontend.Mvc.QnnaireList
{
    public class QnnaireListController : AgrQnnaireController
    {
        public ActionResult Index()
        {
            var q = QueryFactory.CreateQuery<QueryQuestionnaireInList>();
            var model = new ViewModel { List = q.Results() };
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult NewQuestionnaire(string name)
        {
            CommandIssuer.IssueCommand(
                new CreateEmptyQuestionnaire
                {
                    AffectedAggregateRootId = Guid.NewGuid(),
                    Name = name,
                });
            return null;
        }

        public ActionResult UpdatedViewModel()
        {
            return Json(new ViewModel {
                List = QueryFactory.CreateQuery<QueryQuestionnaireInList>().Results()
            }, JsonRequestBehavior.AllowGet);
        }
    }
}
